package com.lyn.pluginbazaar.controller;

import com.lyn.pluginbazaar.annotation.Login;
import com.lyn.pluginbazaar.api.FileAdapter;
import com.lyn.pluginbazaar.entity.Result;
import com.lyn.pluginbazaar.service.FileService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Api(tags = "文件")
@CrossOrigin
@RestController
@RequestMapping(value = "/file")
public class FileController implements FileAdapter {

    @Autowired
    private FileService fileService;


    @Login
    @PostMapping(value = "/upload")
    @Override
    public Result uploadPlugin(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response) {
        return fileService.uploadPlugin(file, request, response);
    }

    @Login
    @GetMapping(value = "/delete")
    @Override
    public Result delete(@RequestParam("fileName") String fileName) {
        return fileService.delete(fileName);
    }

    @GetMapping(value = "/download")
    @Override
    public void download(@RequestParam("fileName") String fileName, HttpServletRequest request, HttpServletResponse response) {
        fileService.download(fileName, request, response);
    }

    @GetMapping(value = "/getImage")
    @Override
    public void getImage(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        fileService.getImage(fileName, response);
    }
}
